import {Trigger} from "solar/w3ts/handles/trigger";
import BaseUtil from "../../solar/util/BaseUtil";
import ForceUtil from "../../solar/util/ForceUtil";
import CreeperAttackWaveState from "./CreeperAttackWaveState";
import UnitUtil from "../../solar/util/UnitUtil";
import {MapPlayer} from "../../solar/w3ts/handles/player";
import {Unit} from "../../solar/w3ts/handles/unit";
import FailState from "./FailState";
import DataBase from "../../solar/common/DataBase";
import SelectUtil from "../../solar/util/SelectUtil";

/**
 * Combat power 战力
 * Endless Level 无尽等级
 */
export default class BaseMultiboardState {
    /**
     * config
     */
    static config = {
        headRowCount: 1,
        showGold: true,
        showLumber: true,
        showCombatPower: false,
        showEndlessLevel: true,

    }


    static multiboard: multiboard = null;
    static CreeperCount: number = 0;


    constructor() {
        BaseUtil.runLater(3, () => {
            this.initVars();
            this.init();
            this.initTimerEventTrigger();
            this.initKillCountTrigger();

        })

    }

    initTimerEventTrigger() {
        let trigger = new Trigger()
        trigger.registerTimerEvent(5, true)
        trigger.addAction(() => {
            this.refreshCreeperCount()
            this.refreshCombatPower()
        })
    }

    refreshCreeperCount() {
        //计算怪物数
        let count = 0
        SelectUtil.forPlayerUnits(u => {
            count++;
        }, 10)
        SelectUtil.forPlayerUnits(u => {
            count++;
        }, 11)
        BaseMultiboardState.CreeperCount = count
    }


    refreshCombatPower() {

        ForceUtil.forUserForce(player => {
            let combatPower = 0;
            SelectUtil.forPlayerUnits(u => {
                let unit = Unit.fromHandle(u);
                if (unit.isHero()) {
                    combatPower = combatPower + unit.strength + unit.agility + unit.intelligence;
                }
            }, GetPlayerId(player))
            MapPlayer.fromHandle(player).solarData.combatPower = Math.floor(combatPower);
        })
    }


    initKillCountTrigger() {
        let trigger = new Trigger()
        trigger.registerAnyUnitDeathEvent()
        trigger.addAction(() => {
            let player = GetOwningPlayer(GetKillingUnit());
            if (IsHandle(player) && GetOwningPlayer(GetTriggerUnit()) != player) {
                //太阳自定义数据
                let solarData = MapPlayer.fromHandle(player).solarData;
                let kill_count = solarData.kill_count;
                if (kill_count && kill_count > 0) {
                    kill_count++;
                } else {
                    kill_count = 1;
                }
                solarData.kill_count = kill_count
            }
        })
    }


    initVars() {
        BaseMultiboardState.multiboard = CreateMultiboard();
        //
        MultiboardSetRowCount(BaseMultiboardState.multiboard, ForceUtil.getUserCount() + BaseMultiboardState.config.headRowCount);
        MultiboardSetItemsWidth(BaseMultiboardState.multiboard, 0.05);
        MultiboardSetColumnCount(BaseMultiboardState.multiboard, 6);
        MultiboardMinimize(BaseMultiboardState.multiboard, false);
        MultiboardSetItemsStyle(BaseMultiboardState.multiboard, true, false);
        MultiboardSetTitleTextColor(BaseMultiboardState.multiboard, 0, 204, 51, 255);

        // =============第一行为标题行==========


        let col = 1;
        MultiboardSetItemValueBJ(BaseMultiboardState.multiboard, col, 1, "玩家名");
        if (BaseMultiboardState.config.showGold) {
            col++;
            MultiboardSetItemValueBJ(BaseMultiboardState.multiboard, col, 1, "黄金");
        }
        if (BaseMultiboardState.config.showLumber) {
            col++;
            MultiboardSetItemValueBJ(BaseMultiboardState.multiboard, col, 1, "木材");
        }
        if (BaseMultiboardState.config.showCombatPower) {
            col++;
            MultiboardSetItemValueBJ(BaseMultiboardState.multiboard, col, 1, "战力");
        }
        col++;
        MultiboardSetItemValueBJ(BaseMultiboardState.multiboard, col, 1, "击杀数");
        if (BaseMultiboardState.config.showEndlessLevel) {
            col++;
            MultiboardSetItemValueBJ(BaseMultiboardState.multiboard, col, 1, "漏怪数");
        }
        MultiboardSetColumnCount(BaseMultiboardState.multiboard, col);
        for (let i = 1; i <= col; i++) {
            MultiboardSetItemColorBJ(BaseMultiboardState.multiboard, i, 1, 0.00, 80, 20, 0);
        }
        MultiboardDisplay(BaseMultiboardState.multiboard, true);
        //30秒内一直轮询 显示 以免被其他对话框阻碍
        BaseUtil.runLater(3, () => {
            MultiboardDisplay(BaseMultiboardState.multiboard, true)
        }, 10)

    }

    init() {
        let trigger = new Trigger()
        trigger.registerTimerEvent(1, true)
        trigger.addAction(this.refresh)
    }

    refresh(this: void) {

        let gameTime = BaseUtil.getGameTime();
        let time_str = "时间：" + I2S(gameTime / 60) + ":" + I2S(gameTime % 60)
        let level_str = "波数" + "：" + CreeperAttackWaveState.level
        // let life_str = "剩余漏怪数" + "：" + FailState.life + "/" + FailState.lifeMax
        let title = time_str + "                             " + level_str + "                             "
        if (gameDifficulty > 1) {
            title = title + "                            难度：" + gameDifficulty
        }

        MultiboardSetTitleText(BaseMultiboardState.multiboard, title);
        // =============玩家数据==========

        let i = 0;
        ForceUtil.forUserForce(player => {
            i++;
            let row = i + BaseMultiboardState.config.headRowCount;
            let col = 1;
            MultiboardSetItemValueBJ(BaseMultiboardState.multiboard, col, row, GetPlayerName(player));
            if (BaseMultiboardState.config.showGold) {
                col++;
                MultiboardSetItemValueBJ(BaseMultiboardState.multiboard, col, row, GetPlayerState(player, PLAYER_STATE_RESOURCE_GOLD) + "");
            }
            if (BaseMultiboardState.config.showLumber) {
                col++;
                MultiboardSetItemValueBJ(BaseMultiboardState.multiboard, col, row, GetPlayerState(player, PLAYER_STATE_RESOURCE_LUMBER) + "");
            }
            if (BaseMultiboardState.config.showCombatPower) {
                col++;
                MultiboardSetItemValueBJ(BaseMultiboardState.multiboard, col, row, MapPlayer.fromHandle(player).solarData.combatPower);
            }
            col++;
            MultiboardSetItemValueBJ(BaseMultiboardState.multiboard, col, row, "" + MapPlayer.fromHandle(player).solarData.kill_count);
            if (BaseMultiboardState.config.showEndlessLevel) {
                col++;
                let solarData = DataBase.getPlayerSolarData(player);
                if (!solarData.FailState_LouGuai_Count) {
                    solarData.FailState_LouGuai_Count = 0;
                }
                MultiboardSetItemValueBJ(BaseMultiboardState.multiboard, col, row, solarData.FailState_LouGuai_Count + "/30");
            }


        })


    }


}
